package com.aa.setting;

import org.json.JSONException;
import org.json.JSONObject;

import com.aa.Common;
import com.aa.R;
import com.aa.http.HttpOperation;
import com.aa.http.RequestParams;
import com.aa.http.RequestTask;
import com.aa.http.RequestTaskInterface;
import android.R.integer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class BangDingActivity extends Activity  {

	private final String mUrl = "http://121.9.64.196:8087/AAproject/api/mobile.php";
	private String TAG = "BangDingActivity";
	private EditText metPhoneNumber;
	private EditText metPassword;
	private EditText metEnsurePwd;
	private Button   mbtnBack;
	private Button   mbtnOk;
	private CheckBox   mbtnBangding;
	private CheckBox   mbtnRegister;
	private TextView mtvTitleCenter;
	private String msPnoneNumber;
	private String msPassword;
	private String msAgentID;
	private String mErrorInfo;
	private RequestParams mReturnParam;
	private String mRegisterUrl;
	private boolean flage = false;
	private Handler mHandler;
	ProgressDialog mProgressDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bangding);
		initView();
		initListener();
		
		mHandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {				
				super.handleMessage(msg);
				switch (msg.what) {
				case 0:
				
						
						Toast.makeText(BangDingActivity.this, mErrorInfo, 3000).show();
					
				case 1:
					Common.saveUserInfo(BangDingActivity.this);
					if (!flage) {
						Toast.makeText(BangDingActivity.this, R.string.register_info_21, 3000).show();
						return;
					}else {
						Toast.makeText(BangDingActivity.this, R.string.register_info_20, 3000).show();
						return;
					}

				default:
					break;
				}
			}
			
		};
	}
	
	private void initView(){
		metPhoneNumber = (EditText)findViewById(R.id.bangding_edit1);
		metPassword    = (EditText)findViewById(R.id.bangding_edit2);
		metEnsurePwd	   = (EditText)findViewById(R.id.bangding_edit_agent);
		mtvTitleCenter = (TextView)findViewById(R.id.app_title_center);
		mtvTitleCenter.setText(R.string.register_info_18);
		mbtnBack       = (Button)findViewById(R.id.title_btn1);
		mbtnBack.setVisibility(1);
		mbtnBack.setText(R.string.app_back_tip);
		mbtnOk   = (Button)findViewById(R.id.title_btn4);
		mbtnOk.setVisibility(1);
		mbtnOk.setText(R.string.app_ok);
		mbtnBangding = (CheckBox)findViewById(R.id.btn_bangding);
		mbtnRegister = (CheckBox)findViewById(R.id.btn_register);
		mbtnBangding.setChecked(true);
		if (Common.iMyPhoneNumber != null && Common.iMyPhoneNumber.length() > 0) {
			metPhoneNumber.setText(Common.iMyPhoneNumber);
		}
	}
	
	private void initListener(){
		mbtnBack.setOnClickListener(new OnDangDingClickListner());
		mbtnOk.setOnClickListener(new OnDangDingClickListner());
		mbtnBangding.setOnClickListener(new OnDangDingClickListner());
		mbtnRegister.setOnClickListener(new OnDangDingClickListner());
			
	
	}
	
	private class OnDangDingClickListner implements OnClickListener{

		@Override
		public void onClick(View v) {
			int _ViewID = v.getId();
			switch (_ViewID) {
			case R.id.title_btn1:
				finish();
				break;
			case R.id.title_btn4:
				bangDingOrRegister();
				break;
			case R.id.btn_bangding:
				mtvTitleCenter.setText(R.string.register_info_18);
				findViewById(R.id.tv_ensure_pwd).setVisibility(View.GONE);
				metEnsurePwd.setVisibility(View.GONE);
				flage = false;
				mbtnBangding.setChecked(true);
				mbtnRegister.setChecked(false);
				break;
			case R.id.btn_register:
				mtvTitleCenter.setText(R.string.register_info_19);
				findViewById(R.id.tv_ensure_pwd).setVisibility(View.VISIBLE);
				metEnsurePwd.setVisibility(View.VISIBLE);
				flage = true;
				mbtnBangding.setChecked(false);
				mbtnRegister.setChecked(true);
				break;
			default:
				break;
			}
			
		}
		
	}
	
	private void bangDingOrRegister(){
		msPnoneNumber = metPhoneNumber.getText().toString();
		if (msPnoneNumber.length() == 0) {
			Toast.makeText(BangDingActivity.this, R.string.bangding_7, 3000).show();
			return;
		}
		
		if (msPnoneNumber.equals(Common.iMyPhoneNumber)) {
			Toast.makeText(BangDingActivity.this, R.string.app_has_logged_on, 3000).show();
			return;
		}
		
		msPassword = metPassword.getText().toString();
		if (msPassword.length() == 0) {
			Toast.makeText(BangDingActivity.this, R.string.bangding_8, 3000).show();
			return;
		}
		
		if (flage) {
			String _EnsurePwd = metEnsurePwd.getText().toString();
			if (!msPassword.equals(_EnsurePwd)) {
				Toast.makeText(BangDingActivity.this, R.string.bangding_14, 3000).show();
				metPassword.setText("");
				metEnsurePwd.setText("");
				return;
			}
			sumbitRegister(msPnoneNumber,msPassword,true);
		
		}else {
			sumbitBangDing(msPnoneNumber, msPassword, true);
		}
		
		
	}
	
	private boolean checkAgentID(){
		String str  = metEnsurePwd.getText().toString();
		if (str != null && str.length() == 11 && ((str.startsWith("13")) || (str.startsWith("15")) || (str.startsWith("15")))) {
			msAgentID = str;
			return true;
		}
		return false;
	}
	
	private void sumbitBangDing(String pNumber, String pPassword,boolean pIsDialog){
		
		if (pIsDialog) {
			mProgressDialog = ProgressDialog.show(this, getString(R.string.app_progress_title), 
					getString(R.string.app_progress_tip),true,false);
			mProgressDialog.setCancelable(true);
		}
		
		final JSONObject _SendJson = new JSONObject();
		JSONObject _BodyJson = new JSONObject();
		JSONObject _HeadJson = new JSONObject();
		try {
			_HeadJson.put("v", "1.0.0");
			_HeadJson.put("time", System.currentTimeMillis());
			_HeadJson.put("cmd", 1);
			_HeadJson.put("mac", Common.iMac);
												
			_BodyJson.put("pwd", pPassword);
			_BodyJson.put("pnumber", pNumber);
			_BodyJson.put("uid", pNumber);
		//	_BodyJson.put("agent", msAgentID);
			
			_SendJson.put("head", _HeadJson);
			_SendJson.put("body", _BodyJson);
			Log.i(TAG, "sendJson = " + _SendJson);
		} catch (JSONException e) {
			
			e.printStackTrace();
		}
		
		new Thread(new Runnable() {			
			@Override
			public void run() {
				
				String _Result = HttpOperation.postRequestJSONFormat(mUrl, _SendJson.toString());
				mProgressDialog.dismiss();
				Log.i(TAG, "return _Result = " + _Result);
				if ( _Result == null || _Result.length() == 0) {
					return;
				}
				
				if (_Result.equals("ConnectTimeout")) {
					return;
				}
				
				
				if (_Result != null) {
					
					try {
						JSONObject _JsonObject = new JSONObject(_Result);
						JSONObject _HeadJson = _JsonObject.getJSONObject("head");
						JSONObject _BodyJson = _JsonObject.getJSONObject("body");
						
						if(_BodyJson.getInt("rcode") == 1) {
							Common.iVersion = (String)_HeadJson.get("v");
							Common.iMyPhoneNumber = msPnoneNumber;
							//Common.iAgentId = _HeadJson.getString("agent");
							Common.iUID = _BodyJson.getString("uid");
							Common.iPassword = msPassword;
							Common.iLogin = 1;
							mHandler.sendEmptyMessage(1);
										
							Common.iTitile = _BodyJson.getString("title");
							String[] _url = _BodyJson.getString("url1").split("/");
							
							if (_url.equals("")) {
								Common.iBitmapNames.add( _url[_url.length -1]);
								Common.iCurrFile = _url[_url.length - 2];
							}
							
							
							
							 _url = _BodyJson.getString("url2").split("/");	
							 if (_url.equals("")) {
									Common.iBitmapNames.add( _url[_url.length -1]);
									Common.iCurrFile = _url[_url.length - 2];
								}
							// Common.iBitmapNames.add( _url[_url.length -1]);
																					
							 _url = _BodyJson.getString("url3").split("/");	
							 if (_url.equals("")) {
									Common.iBitmapNames.add( _url[_url.length -1]);
									Common.iCurrFile = _url[_url.length - 2];
								}
							// Common.iBitmapNames.add( _url[_url.length -1]);
							
							 _url = _BodyJson.getString("url4").split("/");	
							 if (_url.equals("")) {
									Common.iBitmapNames.add( _url[_url.length -1]);
									Common.iCurrFile = _url[_url.length - 2];
								}
							// Common.iBitmapNames.add( _url[_url.length -1]);
							finish();
						}else {
							mErrorInfo = _BodyJson.getString("errstr");
							mHandler.sendEmptyMessage(0);
						}
					} catch (JSONException e) {						
						e.printStackTrace();
					}
				}
				
			}
		}).start();
		
	}

	private void sumbitRegister(String pNumber, String pPassword,boolean pIsDialog){
		if (pIsDialog) {
			mProgressDialog = ProgressDialog.show(this, getString(R.string.app_progress_title), 
					getString(R.string.app_progress_tip),true,false);
			mProgressDialog.setCancelable(true);
		}
		
		final JSONObject _SendJson = new JSONObject();
		JSONObject _BodyJson = new JSONObject();
		JSONObject _HeadJson = new JSONObject();
		try {
			_HeadJson.put("v", "1.0.0");
			_HeadJson.put("time", System.currentTimeMillis());
			_HeadJson.put("cmd", 7);
			_HeadJson.put("mac", Common.iMac);
			_HeadJson.put("agent", Common.iAgentId);
									
			_BodyJson.put("pwd", pPassword);
			_BodyJson.put("pnumber", pNumber);
		
			
			_SendJson.put("head", _HeadJson);
			_SendJson.put("body", _BodyJson);
			Log.i(TAG, "sendJson = " + _SendJson);
		} catch (JSONException e) {
			
			e.printStackTrace();
		}
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				String _Result = HttpOperation.postRequestJSONFormat(mUrl, _SendJson.toString());
				Log.i(TAG, "return _Result = " + _Result);
				if ( _Result == null || _Result.length() == 0) {
					return;
				}
				if (_Result != null) {
					if (mProgressDialog != null) {
						mProgressDialog.dismiss();
					}
					try {
						JSONObject _JsonObject = new JSONObject(_Result);
						JSONObject _HeadJson = _JsonObject.getJSONObject("head");
						JSONObject _BodyJson = _JsonObject.getJSONObject("body");
						
						if(_BodyJson.getInt("rcode") == 1) {
							Common.iVersion = (String)_HeadJson.get("v");
							Common.iMyPhoneNumber = msPnoneNumber;
							
							Common.iUID = _BodyJson.getString("uid");
							Common.iPassword = msPassword;
							Common.iLogin = 1;
							mHandler.sendEmptyMessage(1);
							finish();
						}else {
							mErrorInfo = _BodyJson.getString("errstr");
							mHandler.sendEmptyMessage(0);
						}
					} catch (JSONException e) {						
						e.printStackTrace();
					}
				}
				
			}
		}).start();
	}
	

}
